package com.zline.foodraise.view;

import android.content.Context;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.StyleSpan;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zline.foodraise.R;
import com.zline.foodraise.entity.TradingBean;
import com.zline.foodraise.util.TimeUtil;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

/**
 * Created by heshantao on 15/11/12.
 * 交易明细Item
 */
@EViewGroup(R.layout.trading_list_item)
public class TradingItem extends RelativeLayout {
    private static final String LOG_TAG = TradingItem.class.getSimpleName();
    Context cxt;
    //交易类型
    @ViewById(R.id.trading_type_tv)
    TextView trading_type_tv;
    //交易时间
    @ViewById(R.id.trading_time_tv)
    TextView time_tv;
    //交易金额
    @ViewById(R.id.trading_amount_tv)
    TextView trading_amount_tv;
    //充值
    @StringRes(R.string.top_up)
    String top_up;
    //支出
    @StringRes(R.string.expense)
    String expense;
    //退款
    @StringRes(R.string.refund)
    String refund;
    //分享获利
    @StringRes(R.string.profit_after_share)
    String profit;
    @Bean
    TimeUtil timeUtil;

    public TradingItem(Context context) {
        super(context);
        this.cxt = context;
    }

    public void bind(TradingBean tradingBean) {
        if (tradingBean != null) {
            setTradingType(tradingBean);
            String time=timeUtil.formatData3(tradingBean.getCreateTimeStamp());
            time_tv.setText(time);
            setTradingAmount(tradingBean);
        }
    }

    //交易类型(1自己充值;2他人充值;3筹支出;4筹退款;5;吃支出;6吃退款;7商品分销奖励)
    void setTradingType(TradingBean tradingBean) {
        int trading_type = tradingBean.getOperateType();
        String trading_type_Res="";
        switch (trading_type) {
            case 1:
                trading_type_Res=top_up;
                break;
            case 2:
                trading_type_Res=top_up;
                break;
            case 3:
                trading_type_Res=expense;
                break;
            case 4:
                trading_type_Res=refund;
                break;
            case 5:
                trading_type_Res=expense;
                break;
            case 6:
                trading_type_Res=refund;
                break;
            case 7:
                trading_type_Res=profit;
                trading_type_tv.setTextColor(cxt.getResources().getColor(R.color.red));
                break;
            default:
                break;
        }
        trading_type_tv.setText(trading_type_Res);
    }

    //交易金额
    void setTradingAmount(TradingBean tradingBean){
        double amount=tradingBean.getAmount();
        String amountRes="";
        if(amount>0){
            amountRes="+"+amount;
            trading_amount_tv.setTextColor(cxt.getResources().getColor(R.color.red));
        }else{
            amountRes=""+amount;
        }
        SpannableString  spannableStr=new SpannableString(amountRes);
        spannableStr.setSpan(new StyleSpan(Typeface.BOLD),1,2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStr.setSpan(new AbsoluteSizeSpan(16,true),1,2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        trading_amount_tv.setText(spannableStr);
    }
}
